﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;

namespace project_assess.assess_relate
{
    public partial class material_frm :Office2007Form
    {
        Mfrm mfrm;
        DataBase m_db;
        public material_frm(Mfrm frm)
        {
            InitializeComponent();
            mfrm = frm;
            m_db = mfrm.m_db;
            load_prj_material_tree();
        }


        //选择工程
        private void btn_chooseprj_Click(object sender, EventArgs e)
        {
            choose_prj frm = new choose_prj(mfrm);
            frm.ShowDialog(this);
            load_prj_material_tree();
        }

        //加载工程的材料考核记录
        private void load_prj_material_tree()
        {
            if(mfrm.curprjid==""  ||  mfrm.curprjid== null)
            {
                label.Text = "尚未选择考核项目";
                return;
            }
            string sql;
            sql = "select [材料费用],[次数] from [assess_material] where [项目编号]='" + mfrm.curprjid +
                "' order by [次数]";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            tree.Nodes.Clear();
            Node n = new Node();
            n.Expanded = true;
            n.Text = mfrm.curprjid;
            int j = 1;
            double sum = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node nn = new Node();
                nn.Text = "第"+dt.Rows[i]["次数"].ToString()+"次";
                nn.Cells.Add(new Cell(dt.Rows[i]["材料费用"].ToString()));
                sum += Convert.ToDouble(dt.Rows[i]["材料费用"]);
                n.Nodes.Add(nn);
                j++;
            }
            tree.Nodes.Add(n);
            sql = "select [材料埋设] from [projects] where [项目编号]='" + mfrm.curprjid + "'";
            dt = m_db.GetDataSet(sql).Tables[0];
            label.Text = "\r\n测算材料埋设费共:" + dt.Rows[0][0].ToString() + "\r\n已考核:" + sum + "\r\n剩余:"
                + (Convert.ToDouble(dt.Rows[0][0]) - sum).ToString();
            labelX2.Text = "请输入第" + j.ToString() + "次材料埋设费";
              sql = "select [项目提成],[班组提成] from [assess_material] where [项目编号]='" + mfrm.curprjid +
                 "' and [次数]=1";
            dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
                label_result.Text = "未核算";
            }
            else
            {
                double left = Convert.ToDouble(dt.Rows[0][0]);
                double index = Convert.ToDouble(dt.Rows[0][1]);
                if (index >0)
                {
                    label_result.Text = "核算结果,项目提成:" + left + ",班组提成:" + index;
                }
                else if (index < 0)
                {
                    label_result.Text = "核算结果,班组工资需要扣除:" + index;
                }
                else
                {
                    label_result.Text = "未核算";
                }
            }
        }


        //添加材料考核费用
        private void btn_material_Click(object sender, EventArgs e)
        {
            message_frm frm;
            Node n = tree.Nodes[0];
            if (mfrm.curprjid == "" || mfrm.curprjid == null)
            {
                frm = new message_frm("没有选择考核工程", "错误", icon_img.alert1);
                frm.ShowDialog(this);
                frm.Close();
                return;
            }
            string s = "select Max([次数]) from [assess_material] where [项目编号]='" + mfrm.curprjid + "'";
            DataTable dt = m_db.GetDataSet(s).Tables[0];

            int no=1;
            try { no = (int)dt.Rows[0][0] + 1; }
            catch { }
            frm = new message_frm("是否要添加材料埋设费用?", "确认", icon_img.query);
            frm.ShowDialog(this);
            if (frm.yesorno == true)
            {
                string sql = "insert into [assess_material] values('" + mfrm.curprjid + "',"
                    + material_fee.Value + "," + no + ",'','','"
                    + DateTime.Now.ToShortDateString() + "')";
                m_db.ExecuteSQL(sql);
            }
            frm.Close();
            load_prj_material_tree();
        }

        //开始核算
        private void btn_assess_Click(object sender, EventArgs e)
        {
            string sql;
            message_frm frm;
            sql = "select sum([材料费用]) from [assess_material] where [项目编号]='" + mfrm.curprjid +
                "'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            double sum = Convert.ToDouble(dt.Rows[0][0]);
            sql = "select [班组提成] from [assess_material] where [项目编号]='" + mfrm.curprjid +
                 "' and [次数]=1";
            dt = m_db.GetDataSet(sql).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {
              
            }
            else
            {
                double index = Convert.ToDouble(dt.Rows[0][0]);
                if (index != 0)
                {
                    frm = new message_frm("已经完成核算,\r\n是否删除原有记录?", "询问", icon_img.query);
                    frm.ShowDialog(this);
                    if (frm.yesorno == false)
                    {
                        frm.Close();
                        return;
                    }
                }
            }
            sql = "select [材料埋设] from [projects] where [项目编号]='" + mfrm.curprjid + "'";
            dt = m_db.GetDataSet(sql).Tables[0];
            double total = Convert.ToDouble(dt.Rows[0][0]);

            double left = total - sum;
            frm = new message_frm("是否要核算材料埋设费用?", "询问", icon_img.query);
            frm.ShowDialog(this);
            if (frm.yesorno == false)
            {
                frm.Close();
                return;
            }
            frm.Close();
            if (left < 0)
            {
                sql = "update [assess_material] set [班组提成]=" + left + " where [项目编号]='"
                    + mfrm.curprjid + "' and [次数]=1";
                m_db.ExecuteSQL(sql);
                label_result.Text = "核算结果,班组工资需要扣除:" + left;
                
            }
            else
            {
                double temp = left * 0.5 * 0.3;
                sql = "update [assess_material] set [项目提成]=" + temp + " where [项目编号]='"
                  + mfrm.curprjid + "' and [次数]=1";
                m_db.ExecuteSQL(sql);
                left = left * 0.5 * 0.7;
                sql = "update [assess_material] set [班组提成]=" + left + " where [项目编号]='"
                  + mfrm.curprjid + "' and [次数]=1";
                m_db.ExecuteSQL(sql);
                label_result.Text = "核算结果,项目提成:" + temp + ",班组提成:" + left;
            }
        }

        //删除功能
        private void tree_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete)
            {
                Node n =
                tree.SelectedNode;
                if (n.Level > 0)
                {
                    message_frm frm;
                    frm = new message_frm("是否要删除?", "询问", icon_img.query);
                    frm.ShowDialog(this);
                    if (frm.yesorno == true)
                    {
                        string text = n.Text;
                        text = text.Replace("第", "");
                        text = text.Replace("次", "");
                        int no = Convert.ToInt32(text);
                        string sql = "delete from [assess_material] where [项目编号]='" + mfrm.curprjid +
                            "' and [次数]=" + no;
                        m_db.ExecuteSQL(sql);
                        load_prj_material_tree();
                    }
                    frm.Close();
                }
            }
        }


    }
}
